/*
 *
 * Copyright (c) 2001-2018 泛微软件.
 * 泛微协同商务系统,版权所有.
 * 
 */
package com.engine.demo.cmd;

import com.cloudstore.eccom.constant.WeaMessageCode;
import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.constant.*;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.demo.dao.DemoDao;
import weaver.conn.DBUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @version 1.0
 * @author AndyZhang
 * TODO 
 */
public class DoDeleteDemoCmd extends AbstractCommonCommand<Map<String,Object>> {

	private final static Log log = LogFactory.getLog(DoDeleteDemoCmd.class);
	
    private BizLogContext bizLogContext = new BizLogContext();

    public DoDeleteDemoCmd(Map<String,Object> params,User user) {
        this.params = params;
        this.user = user;
    }

    @Override
    public BizLogContext getLogContext() {
        String ruleid = Util.null2String(params.get("id"));
        String _flagInfo = "删除";
        bizLogContext.setDateObject(new Date());
        bizLogContext.setUserid(user.getUID());
        bizLogContext.setUsertype(Util.getIntValue(user.getLogintype()));
        bizLogContext.setTargetId(ruleid);
        bizLogContext.setLogType(BizLogType.SYSTEM);
        bizLogContext.setLogSmallType(BizLogSmallType4SysEngine.SYSTEM_ENGINE_EM);
        bizLogContext.setOperateType(BizLogOperateType.DELETE);
        bizLogContext.setParams(params);
        bizLogContext.setClientIp(Util.null2String(params.get(ParamConstant.PARAM_IP)));
        bizLogContext.setDesc(String.format(user.getLastname() + "对：规则管理做了"+_flagInfo+"操作， "+_flagInfo+"的mapid是：{"+ruleid+"}  "));
        return bizLogContext;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
    	WeaResultMsg result=new WeaResultMsg(true);

        //批量删除，前台返回的是ids，存放多个要删除的id
    	boolean hasRight = HrmUserVarify.checkUserRight("demo:delete", user);
    	if (hasRight)
    	{
    		String ids = Util.null2String(params.get("ids"));
            List<String> params=new ArrayList<>();
            Object[] objects=DBUtil.transListIn(ids,params);
            RecordSet rs = new RecordSet();
            String sql = DemoDao.getDelete(objects[0]);
            boolean i=rs.executeUpdate(sql,params);
            result.success();
    	} else {
    		result.fail("noRight", WeaMessageCode.RT_NORIGHT.getCode());
    	}
        return result.getResultMapAll();
    }
}
